package com.huage_05.lesson_02;

public class ArrayDemo {
    public static void main(String[] args) {

        int[] arr = {12,98,37,46,67};
        System.out.println("排序前数组内容:"+arrayToString(arr));
        //冒泡排序
        for (int j=0;j<arr.length-1;j++) { //做 n-1 次比较
            for (int i = 0; i < arr.length -1-j;i++) {
                if(arr[i]>arr[i+1]){
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                }
            }
        }
        System.out.println("排序后数组内容:"+arrayToString(arr));
    }
    //将数组按指定规则输出
    /*
        返回值类型：String
        参数：int[] arr
     */
    public static String arrayToString(int[] arr){
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i=0;i<arr.length;i++){
            if (i==arr.length-1){
                sb.append(arr[i]);
            }else {
                sb.append(arr[i]).append(",");
            }
        }
        sb.append("]");
        String str = sb.toString();
        return str;
    }
}
